"use strict";
const { Model, Sequelize } = require("sequelize");
const db = require(".");
module.exports = (sequelize, DataTypes) => {
  class Project extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Project.belongsTo(models.Staff, {
        foreignKey: "manager_id",
        as: "manager",
        allowNull: true,
      });
      Project.belongsTo(models.Client, {
        foreignKey: "client_id",
        allowNull: true,
      });
      Project.hasOne(models.Contract, {
        foreignKey: "project_id",
      });
      Project.hasMany(models.Implement, {
        foreignKey: "project_id",
      });
      Project.hasMany(models.Receipt, {
        foreignKey: "project_id",
      });
      Project.hasOne(models.Service, {
        foreignKey: "project_id",
      });
      Project.belongsTo(models.Staff, {
        foreignKey: "checker_id",
        as: "checker",
        allowNull: true,
      });

      // define association here
    }
  }
  Project.init(
    {
      number: DataTypes.STRING,
      project_name: DataTypes.STRING,
      date_start: {
        type: DataTypes.DATEONLY,
        defaultValue: Sequelize.NOW,
      },
      date_end: {
        type: DataTypes.DATEONLY,
        defaultValue: Sequelize.NOW,
      },
      remarks: DataTypes.STRING, //备注
      project_status: DataTypes.STRING,
      approval_status: {
        type: DataTypes.STRING,
        defaultValue: "待审核",
      }, //审核状态
      manager_id: DataTypes.INTEGER,
      client_id: DataTypes.INTEGER,
      check_sug: DataTypes.STRING,
      // check_staffs:DataTypes.TEXT
    },
    {
      sequelize,
      modelName: "Project",
      tableName: "projects",
    }
  );
  return Project;
};
